﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Revis.VClinicNet.Core.Domain
{
    public class Doctor : Worker, IEquatable<Doctor>
    {
        public virtual IList<Visit> Visits { get; protected set; }
        public virtual WorkSchedule WorkSchedule { get; protected set; }

        private IDateTimeService _date = null;

        #region Contructors

        public Doctor()
        {
            _date = new ActualDateTimeService();
        }

        public Doctor(string firstName, string surname)
            : this(0, firstName, surname, null)
        {
        }

        public Doctor(long id, string firstName, string surname) :
            this(id, firstName, surname, null)
        {

        }

        public Doctor(long id, string firstName, string surname, IDateTimeService dateTimeService) :
            base(id, firstName, surname)
        {
            Visits = new List<Visit>();
            WorkSchedule = new WorkSchedule();
            foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek))) {
                WorkSchedule.Set(day, new WorkTime(day, 8, 0, 16, 0, "brak", true, this));
            }
            _date = dateTimeService ?? new ActualDateTimeService();
        }

        #endregion

        #region Public Methods

        public virtual void SetWorkTime(WorkTime workTime)
        {
            if (workTime == null) {
                throw new ArgumentNullException("Null workTime");
            }
            WorkSchedule.Set(workTime.Day, workTime);
        }

        public virtual WorkTime GetWorkTime(DayOfWeek day)
        {
            return WorkSchedule.Get(day);
        }

        public virtual Entry CheckUp(Visit visit, EntryContent content)
        {
            if (visit == null) {
                throw new ArgumentNullException("Null visit");
            }
            if (content == null) {
                throw new ArgumentNullException("Null content");
            }
            if (visit.Doctor.Id != this.Id) {
                throw new ArgumentException("Nie można wypisać karty");
            }
            Patient patient = visit.Patient;
            Entry entry = new Entry(this, patient, _date.Now(), content);
            patient.Card.Add(entry);
            return entry;
        }

        public virtual void AddVisit(Visit visit)
        {
            if (visit == null) {
                throw new ArgumentNullException("Null visit");
            }
            if (visit.Doctor.Id != Id) {
                throw new ArgumentException("Nie można dodać wizyty");
            }
            Visits.Add(visit);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public virtual bool Equals(Doctor other)
        {
            return base.Equals(other);
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode() ^ base.GetHashCode();
        }

        #endregion


    }
}
